<?php 
	define(SEPARATE, '/');

	echo '<div class="floattop"><div class="itemtitle"><h3>视频购买记录</h3>';
	echo '<ul><li><a href="admin.php?action=buyinfo&operation=add" style="text-decoration:none;"><span style="color:#666;">添加</span></a></li></ul>';
	echo '</div></div>';

	if($operation=='manage' || empty($operation)){
		$totalRow = C::t('home_buyinfo')->get_record_count();
        $totalRow = $totalRow['count(id)'];
		$pageSize = 10;
		$totalPage = ceil($totalRow / $pageSize);
		if($_GET['p'] == ''){
			$currentPage = 1;
		}else{
			$currentPage = $_GET['p'];
		}
		$start = ($currentPage - 1) * $pageSize;
		$re = C::t('home_buyinfo')->get_all_record( $start, $pageSize);
        foreach($re as $key => $value){
            $cate_name = C::t('home_videocate')->get_cate_by_id($value['cate_id']);
            $re[$key]['cate_name'] = $cate_name['cate_name'];
        }

		$str = '';
		for ($i=1; $i <= $totalPage; $i++) { 
			if($currentPage != $i)
				$str .= '<a href="admin.php?action=buyinfo&operation=manage&p='.$i.'"><span style="margin:10px;">'.$i.'</span></a>';
			else
				$str .= '<span style="margin:10px;">'.$i.'</span>';
		}

		if(!empty($re)){
			echo '<table border="3">';
			echo '<tr>';
			echo '<th>视频图片</th>';
			echo '<th>视频名称</th>';
			echo '<th>分类名称</th>';
			echo '<th>购买用户</th>';
			echo '<th>购买时间</th>';
			echo '<th>购买方式</th>';
			echo '<th>支付方式</th>';
			echo '<th>操作</th>';
			foreach($re as $val){
				echo '<tr>';
				echo '<td align="center" style="padding:10px;"><img src="'.$val['v_image'].'" style="width:50px;height:50px;"></td>';
				echo '<td align="center" style="padding:15px;">'.$val['v_name'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['cate_name'].'</td>';
				echo '<td align="center" style="padding:15px;">'.$val['username'].'</td>';
				echo '<td align="center" style="padding:15px;">'.date('Y-m-d H:i:s',$val['buy_time']).'</td>';
                if($val['buy_type'] == 'front'){
                    echo '<td align="center" style="padding:15px;">前台购买</td>';
                }else{
                    echo '<td align="center" style="padding:15px;">后台购买</td>';
                }
                if($val['pay_type'] == 'alipay'){
                    echo '<td align="center" style="padding:15px;">支付宝</td>';
                }else{
                    echo '<td align="center" style="padding:15px;">'.$val['pay_type'].'</td>';
                }
				echo '<td align="center" style="padding:15px;"><a href="admin.php?action=buyinfo&operation=del&id='.$val['id'].'" style="color:red;">删除</a>';
				echo '</tr>';
			}
			echo '<tr><td colspan="11" align="center">'.$str.'</td></tr></table>';
		}
	}elseif($operation == 'del' && $_GET['id'] != ''){
		if(!is_numeric($_GET['id'])){
			echo '<script type="text/javascript">alert("非法参数");</script>';
		}else{
			$info = C::t('home_buyinfo')->get_record_by_id($_GET['id']);
			if(empty($info)){
				echo '<script type="text/javascript">alert("非法参数");</script>';
			}else{
				C::t('home_buyinfo')->del_record($_GET['id']);
				echo '<script type="text/javascript">alert("删除成功");';
				echo 'window.location="'.ADMINSCRIPT.'?action=buyinfo&operation=manage";';
				echo '</script>';
			}
		}
	}elseif($operation == 'add'){
		$videos = C::t('home_video')->get_all_video(0, 0, 0);
		$members = C::t('common_member')->fetch_all_members();
		echo '<form action="admin.php?action=buyinfo&operation=checkAdd" method="post">';
		echo '<table><tr><td>视频名称</td>';
		echo '<td><select name="v_id"><option value="">请选择</option>';
		foreach($videos as $key=>$val){
			echo '<option value="'.$val['v_id'].'">'.$val['v_name'].'</option>';
		}
		echo '</td></tr>';
		echo '<tr><td>用户名</td>';
		echo '<td><select name="m_id"><option value="">请选择</option>';
		foreach($members as $key=>$val){
			echo '<option value="'.$val['uid'].'">'.$val['username'].'</option>';
		}
		echo '</td></tr>';
		echo '<tr><td colspan="2"><input type="submit" value="添加" class="btn"></td></tr></table>';

	}elseif($operation == 'checkAdd'){
		$data['v_id'] = $_POST['v_id'];
		$data['m_id'] = $_POST['m_id'];
		$data['buy_time'] = time();
		$data['buy_type'] = 'admin';
		$data['del'] = 0;
		C::t('home_buyinfo')->insert_record($data);
		echo '<script type="text/Javascript">alert("添加记录成功!");';
		echo 'window.location="'.ADMINSCRIPT.'?action=buyinfo";';
		echo '</script>';
	}

?>